package com.zxxy.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zxxy.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102600763745";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCHPe/4q8mADxgFDICNOn4mEQIT3/fHv+pxVAdoYeTi5EVNw96XAzky7xOOHRY+fmexGXjyvh1B9HbE1nUXAyGk84fry50e9Ql7swu9pi4TlpGw92eS4D/B5QfJk3qFR8D5x2Hbmc3joZub8m+2x4Edv0MRiODAkNgQ4aRfdE/AOtu+u/Gaj9SDrxwxyx4/1jeyDEG2ec6i7xSb4V3ihhjD773HdxZaUxgkRfIWEFtj5XSgmK6TFr/+I/bZBcVruLZiIJ6VjqpSAbb+UcLY39twyN9reVlWU0tJ8yywshvgQtmP7y+syTwGOHo4egPK0UWo3UFygwDQplxKlogqMjOBAgMBAAECggEANuREykGvf3Kg5Su14ZykJv8wQLDe+QxEM8tPVmalaBIOrwmZBuIgDTRMU5oqtSLJI3CbTqsrZOG8cxg4Xia/ORw/Rs1V8IK0XqjFUyxn9ztd1t4UA+FQ/3neN0rMp9v/z2HFrkQ29PVbG2jDPwSqu1AuAKf10TL4/6EV/khPNk4VvuYsq8eAiCpbjxUPJKLvZlky6bi1fgB5WzZsihfXwME3QxpSTHLsLQzDJlAITR/dBUQD9PA22XZw/sCJqTqj9TeVEchSeBIXXulj634c0gwyEBV4t1yMLxRJKHnaKfuA10FdmlwZEueVaUvMK+XgcUlcCBujH46wSeTEc9z0IQKBgQDMQhjuZ6g/FIzinxqMagXFLGKYiHuAlP36WdrQolXfbMnBNEoMJsFxdOptWhjdoAznYYIrIEDGmkUHiyI+upq3EJONPGgZfvg6BmKlVxFXQjPNBMbzma0N1cNXcklTBn2cAQJc0r9ZsAfb6//FBTWikNVbaCKBiHp9nBurJO++ewKBgQCpgDe6RKZUYPBzsD+3PLuykUhoPzRwLK5u6gZ6JtDQAksYyiIhxeGj6dzdK0Rri5AUx+TDumYRUZKckHj27g3HbGMpbwqnTWlFraEEg7ZCvJ+oQ6t129uPzQgaQp1sCa6njObs6pNcEka2T9qCmu81iqfCw4wLOTems9IcnDpzMwKBgQCKTldDVrz9uZSL/t+jJEcz85HvZrr5nek7k4wuqT0sRCV4xUc+Yx+pC1mRIeikaLNUGRpiyTwTPnJvYS6W+xO30WmXwMdcPDUoVRqGiiYbx5/7urhcSjp7m7rhg2lS5po3ABfxcmJPAHrR10MgzR48LYXKlwCGZTtLwmCZyjDr8QKBgQCSdYuxk2f1ON4L2Jr1hbDZltdt3E8/Lhx7EhQOsEN1uMWS4fWkIQvbSlPhd2VjV/BjRGXTCdjv1gBM8DjVhNS7IFh+kMgvz5/K6UBcWkK+X6XFCtO4vwR0uMDrj+veUm9rr0BKLlauE8yt80nn2GAhb3GNxNHZUNNZY7+FoSiQKQKBgQCWvb+SeWCnNupNQV93wRwGcxeAGAwgKFNSK2Cm5/IM18obTYT6W7iE9uq6CF5ILJZz6aL/iHxqSUbthoWNOwgnwJ6YSmvhWldNs0PEixj3k4zbwZeRwmmrj6l/sFZEwRvjbjjpwYIe5V+zRKpNjaeNCVB2ZiCebVqNM8N9VvP0iw==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhz3v+KvJgA8YBQyAjTp+JhECE9/3x7/qcVQHaGHk4uRFTcPelwM5Mu8Tjh0WPn5nsRl48r4dQfR2xNZ1FwMhpPOH68udHvUJe7MLvaYuE5aRsPdnkuA/weUHyZN6hUfA+cdh25nN46Gbm/JvtseBHb9DEYjgwJDYEOGkX3RPwDrbvrvxmo/Ug68cMcseP9Y3sgxBtnnOou8Um+Fd4oYYw++9x3cWWlMYJEXyFhBbY+V0oJiukxa//iP22QXFa7i2YiCelY6qUgG2/lHC2N/bcMjfa3lZVlNLSfMssLIb4ELZj+8vrMk8Bjh6OHoDytFFqN1BcoMA0KZcSpaIKjIzgQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://member.gulimall.com/payed/notity";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
